package com.gc.quartz.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.*;
import com.gc.business.service.*;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author gc
 */
@Slf4j
@Component("ryTask")
public class RyTask {

    private IElevatorInfoService elevatorInfoService;
    private IUpkeepPlanService upkeepPlanService;
    private IUpkeepInfoService upkeepInfoService;
    private ICommunityManageService communityManageService;
    private ICompanyManageService companyManageService;
    private ISysUserService userService;

    @Autowired
    public RyTask(IElevatorInfoService elevatorInfoService,
                  IUpkeepPlanService upkeepPlanService,
                  IUpkeepInfoService upkeepInfoService,
                  ICommunityManageService communityManageService,
                  ICompanyManageService companyManageService,
                  ISysUserService userService
        ) {
        this.elevatorInfoService = elevatorInfoService;
        this.upkeepPlanService = upkeepPlanService;
        this.upkeepInfoService = upkeepInfoService;
        this.communityManageService = communityManageService;
        this.companyManageService = companyManageService;
        this.userService = userService;
    }



    public void ryParams(String params) {
        log.info("执行有参方法：" + params);
    }

    public void ryNoParams() {
        log.info("执行无参方法");
    }

    /**
     * 查询如果需要保养的电梯进行保养(定时器每天凌晨1点执行一次)
     * @return
     */
    public void saveUpkeepBusinessInfo(){

        //查询系统全部电梯
        List<ElevatorInfo> elevatorList = elevatorInfoService.list();

        for (ElevatorInfo elevatorInfo : elevatorList) {
            //查询对应的电梯保养计划
            UpkeepPlan elevatorPlan = upkeepPlanService.getByElevatorId(elevatorInfo.getElevatorId());

            //最近一次保养时间
            String recentlyTime = elevatorPlan.getRecentlyTime();
            //首次保养时间
            String firstUpkeepTime = elevatorPlan.getFirstUpkeepTime();
            //保养间隔时间
            Integer upkeepInterval = elevatorPlan.getUpkeepInterval();
            //提醒天数
            Integer remindDay = elevatorPlan.getRemindDay();

            if(StringUtils.isEmpty(recentlyTime)){
                recentlyTime = firstUpkeepTime;
            }
            if(upkeepInterval > remindDay){
                upkeepInterval = upkeepInterval - remindDay;
            }else{
                upkeepInterval = remindDay;
            }

            try {
                Date dateByUpKeep = DateUtil.parse(recentlyTime,"yyyy-MM-dd");
                //获取保养时间
                Date upkeepDate = DateUtils.getDate(dateByUpKeep, upkeepInterval);
                if(DateUtils.isToday(upkeepDate.getTime())){
                    //派发保养信息
                    UpkeepInfo upkeepInfo = new UpkeepInfo();
                    upkeepInfo.setElevatorId(elevatorInfo.getElevatorId());
                    upkeepInfo.setTemplateId(elevatorPlan.getTemplateId() == null ? 30L : elevatorPlan.getTemplateId());
                    upkeepInfo.setUpkeepCode("BY"+ IdUtils.getPrimaryKey());
                    upkeepInfo.setElevatorName(elevatorInfo.getElevatorName());
                    upkeepInfo.setIdentifyCode(elevatorInfo.getIdentifyCode());
                    //使用单位
                    List<CommunityManage> communityById = communityManageService.list(new CommunityManage(){{setCommunityId(Long.valueOf(elevatorInfo.getBelongCommunity()));}});
                    upkeepInfo.setUseUnit(communityById.get(0).getCommunityName());
                    //维保单位
                    List<CompanyManage> companyById = companyManageService.list(new CompanyManage(){{setCompanyId(Long.valueOf(communityById.get(0).getCompanyId()));}});
                    upkeepInfo.setMaintenanceUnit(companyById.get(0).getCompanyName());
                    upkeepInfo.setUpkeepType(elevatorPlan.getUpkeepType());
                    upkeepInfo.setUpkeepState("1");//保养状态
                    //维保人
                    String applicant = "";
                    List<SysUser> sysUserList = userService.selectUserListByAttrId(companyById.get(0).getCompanyId());
                    if(sysUserList != null && sysUserList.size() > 0){
                        for (SysUser sysUser : sysUserList) {
                            String s = userService.selectUserRole(sysUser.getUserId());
                            if(s.contains(BusinessConstants.KEEP_ENGINEER)){
                                applicant = applicant + sysUser.getUserId() + ",";
                            }
                        }
                    }else{
                        continue;
                    }
                    upkeepInfo.setApplicant(applicant);
                    //截止时间
                    upkeepInfo.setOverTime(DateUtils.getDate(DateUtil.parseDate(recentlyTime), elevatorPlan.getUpkeepInterval()));
                    upkeepInfo.setLongitude(elevatorInfo.getLongitude());
                    upkeepInfo.setLatitude(elevatorInfo.getLatitude());
                    upkeepInfoService.save(upkeepInfo);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


}
